package com.gz.utils;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import com.gz.bean.XmlBean;

public class ConfigXMLUtils {

	public static Map<String,XmlBean> processXML(String xmlPath) throws Exception {
		
		Map<String,XmlBean> xmlMap=new HashMap();
		
		SAXBuilder builder = new SAXBuilder();
		Document document = builder.build(new File(xmlPath));
		Element root = document.getRootElement();

		Element actionRoot = root.getChild("actions");
		List<Element> actionChildrenList = actionRoot.getChildren();
		for (Element e : actionChildrenList) {
			XmlBean o = new XmlBean();

			String name = e.getAttributeValue("name");
			o.setBeanName(name);

			Element beans = root.getChild("beans");
			List<Element> beanList = beans.getChildren();
			for (Element ex : beanList) {
				if (name.equals(ex.getAttributeValue("name"))) {
					o.setBeanClass(ex.getAttributeValue("class"));
					break;
				}
			}

			String actionPath = e.getAttributeValue("path");
			o.setActionPath(actionPath);

			String actionClass = e.getAttributeValue("class");
			o.setActionClass(actionClass);

			List<Element> forwardList = e.getChildren();
			Map<String, String> forwardMap = new HashMap();
			for (Element el : forwardList) {
				forwardMap.put(el.getAttributeValue("name"), el.getAttributeValue("value"));
			}
			o.setActionForward(forwardMap);
			xmlMap.put(actionPath, o);
		}
		
		return xmlMap;

	}

}
